package org.yunai.swjg.server.module.item;

import org.yunai.swjg.server.module.item.container.CommonBag;
import org.yunai.swjg.server.module.item.vo.Item;
import org.yunai.swjg.server.rpc.message.S_C.S_C_BagItemUpdateReq;
import org.yunai.swjg.server.rpc.message.S_C.S_C_BagUpdateReq;
import org.yunai.swjg.server.rpc.struct.StBagItemUpdate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 背包消息Builder
 * User: yunai
 * Date: 13-4-8
 * Time: 上午11:15
 */
public class BagMessageBuilder {

    private BagMessageBuilder() {
    }

    public static S_C_BagUpdateReq build(CommonBag bag) {
        Collection<Item> items = bag.getAll();
        List<StBagItemUpdate> stItems = new ArrayList<>(items.size());
        for (Item item : items) {
            stItems.add(genStBagItemUpdate(item));
        }
        return new S_C_BagUpdateReq(bag.getBagType().get(), (byte) bag.getCapacity(), stItems,
                bag.getOwner().getId());
    }

    public static S_C_BagItemUpdateReq build(Item item) {
        List<Item> itemList = new ArrayList<>(1);
        itemList.add(item);
        return build(itemList);
    }

    public static S_C_BagItemUpdateReq build(List<Item> items) {
        S_C_BagItemUpdateReq req = new S_C_BagItemUpdateReq();
        List<StBagItemUpdate> stItems = new ArrayList<StBagItemUpdate>(items.size());
        for (Item item : items) {
            stItems.add(genStBagItemUpdate(item));
        }
        req.setItems(stItems);
        return req;
    }

    private static StBagItemUpdate genStBagItemUpdate(Item item) {
//        String feature = item.getFeature() != null ? JSON.toJSONString(item.getFeature()) : "";
        return new StBagItemUpdate(
                (byte) item.getBagType().getIndex(), (byte) item.getIndex(),
                item.getId(), item.getOverlap(), item.getOwnerId(), item.getTemplate().getId()
        );
    }
}
